<?php
/**
 * Created by PhpStorm.
 * User: Shinelon
 * Date: 2021/4/17
 * Time: 17:45
 */

namespace app\admin\controller;


use app\admin\model\Admin;
use app\AdminRest;
use think\App;
use think\facade\Cache;

class Login extends AdminRest
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    public function index()
    {
        return view('index');
    }

    public function login(Admin $admin)
    {
        $username = $this->_param['username'] ?? '';
        $psw = $this->_param['password'] ?? '';
        if (empty($username) || empty($psw))
            return $this->error('请输入用户名或密码');
        $adminInfo = $admin->getAdminInfo($username);
        if ($username != $adminInfo['account'] || $psw != $adminInfo['passwd'])
            return $this->error('用户名或密码错误');
        $token = $this->getToken($adminInfo);
        return $this->success(['token'=>$token]);
    }

    public function logout()
    {
        Cache::delete($this->_token);
        return $this->success('退出成功');
    }
}